package com.alibaba.nacos.naming.service.sms.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.naming.config.InterfaceUrlConfig;
import com.alibaba.nacos.naming.mapper.sms.SMSMapper;
import com.alibaba.nacos.naming.pojo.LinkMan;
import com.alibaba.nacos.naming.service.sms.SMSService;
import com.alibaba.nacos.naming.utils.HttpUtil;
import com.alibaba.nacos.naming.utils.KafkaUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class SMSServiceImpl implements SMSService {
    private static final Logger log = LoggerFactory.getLogger(SMSServiceImpl.class);

    @Autowired(required = false)
    private SMSMapper smsMapper;

    @Autowired
    private HttpUtil utils;

    @Autowired
    private KafkaUtil kafkaUtil;

    @Autowired
    private InterfaceUrlConfig interfaceUrlConfig;

    @Autowired
    private SMSInner smsInner;

    /**
     * 批量发送预警短信
     * @param reqStr
     * @return
     */
    @Override
    public String batchSendWarnMessage(String reqStr) {
        Map<Object,Object> map = new HashMap<>();
        try{
            List<LinkMan> list = smsMapper.qryRelalist();
            if(list == null || list.size() == 0){
                map.put("code", 1001);
                map.put("msg", "没有查询到对应联系人信息");
                return JSON.toJSONString(map);
            }

            JSONObject obj = JSONObject.parseObject(reqStr);
            String context = obj.getString("context");//要发送的短信内容
            for (LinkMan linkMan : list ) {
                smsInner.sendMessage(context, linkMan.getLinkphone());
                linkMan.setContext(context);
                kafkaUtil.sendNacosMsg(linkMan.toString());
            }

            map.put("code", 0);
            map.put("msg", "成功");
            return JSON.toJSONString(map);
        }catch (Exception e){
            log.error("批量短信发送异常",e);
            map.put("code", 0);
            map.put("msg", "批量短信发送异常: " + e.getMessage());
            return JSON.toJSONString(map);
        }
    }

    @Component
    class SMSInner{
        @Async
        public void sendMessage(String context, String accNum) {
            try {
                //拼接请求参数
                String param = "{\n" +
                        "\t\"vcid\": 5,\n" +
                        "\t\"operid\": \"1004\",\n" +
                        "\t\"priority\": 1,\n" +
                        "\t\"flowno\": \"16397"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"))+"\",\n" +
                        "\t\"sysaccount\": \"10000111\",\n" +
                        "\t\"useraccount\": \""+accNum+"\",\n" +
                        "\t\"content\": \""+context+"\",\n" +
                        "\t\"result\": 0\n" +
                        "}\n";
                //接口请求方法
                String method = "sendsms";
                System.out.println("sendMessage==>入参: " + param);
                Map<String,String> map = utils.getURLContentPostUTF8(interfaceUrlConfig.getSmsUrl(), param, "application/json", 3000);
                System.out.println("sendMessage==>出参: " + JSON.toJSONString(map));
            }catch (Exception e){
                log.error("短信发送异常",e);
            }
        }
    }
}
